package org.example.web;


import com.example.fanqie.entity.Chapter;
import com.example.fanqie.entity.Novel;
import com.example.fanqie.entity.User;
import com.example.fanqie.mapper.ChapterMapper;
import com.example.fanqie.mapper.NovelMapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/chapter")
public class chapterDao {


    @Resource
    private ChapterMapper chapterMapper;

    @Resource
    private NovelMapper novelMapper;


    @RequestMapping("add")
    public int addchapter(@RequestBody Chapter chapter){
        int insert = chapterMapper.insert(chapter);
        return  insert;
    }

    @RequestMapping("select")

    public List<Chapter> selectchapter(){
        List<Chapter> chapters = chapterMapper.selectchapter();
        chapters.forEach(record -> {
            Novel novel = novelMapper.selectnovelId(record.getNovelId());
            record.setTitle(novel.getTitle());
        });
        return chapters;

    }

    @RequestMapping("selectlist")
    public List<Chapter> selectchapterlist(@RequestBody Chapter chapter ){
        List<Chapter> chapters = chapterMapper.selectchapterlist(chapter.getNovelId());
        return chapters;

    }

    @RequestMapping("delete")
    public int deletechapter( @RequestBody Chapter chapter){

        int update = chapterMapper.updateById(chapter);

        return update;
    }

    @RequestMapping("selectBychapterId")
    public Chapter selectBychapterId(@RequestBody Chapter chapter){
        Chapter chapter1 = chapterMapper.selectBychapterId(chapter.getChapterId());

        return chapter1;
    }


    @RequestMapping("update")
    public int updateNovel( @RequestBody Chapter chapter){
        int update = chapterMapper.updateById(chapter);

        return update;
    }

}
